package com.farsunset.cim.client.android;

import java.util.ArrayList;
import java.util.List;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;

public class MessageDecoder extends LengthFieldBasedFrameDecoder {

	public MessageDecoder(int maxFrameLength, int lengthFieldOffset,
			int lengthFieldLength) {
		super(maxFrameLength, lengthFieldOffset, lengthFieldLength);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected Object decode(ChannelHandlerContext ctx, Channel channel,
			ChannelBuffer aIn) throws Exception {
		if (aIn.readableBytes() < 5) {
			return null;// (1)
		}
		List<Object> aOut = new ArrayList<Object>();
		CommandCodec.decode(ctx, aIn, aOut);
		if (aOut != null && aOut.size() > 0) {
			return aOut.get(0);
		}
		return null;

	}
}